export default {
	namespaced: true,
	state: () => ({
		//选择的收货地址
		address: JSON.parse(uni.getStorageSync('address') || '{}'), 
		//登录后的 token
		token: uni.getStorageSync('token') || '',
		//用户信息
		userinfo: JSON.parse(uni.getStorageSync('userinfo') || '{}'), 
		//登录后跳转至页面的信息：{openType, from}
		redirectInfo: null,
	}),
	mutations: {
		//更新收货地址
		updateAddress(state, address){
			state.address = address;
			this.commit('m_user/saveToStorage');
		},
		//将收货地址存入本地
		saveToStorage(state){
			uni.setStorageSync('address', JSON.stringify(state.address));
		},
		//更新用户信息
		updateUserInfo(state, userInfo){
			state.userinfo = userInfo;
			this.commit('m_user/saveUserInfoToStorage');
		},
		//将用户信息存入本地
		saveUserInfoToStorage(state){
			uni.setStorageSync('userinfo', JSON.stringify(state.userinfo));
		},
		//更新 token
		updateToken(state, token){
			state.token = token;
			this.commit('m_user/saveTokenToStorage');
		},
		//将 token 存入本地
		saveTokenToStorage(state){
			uni.setStorageSync('token', state.token);
		},
		//更新登录后跳转至页面的信息
		updateRedirectInfo(state, info){
			state.redirectInfo = info;
			console.log('info:');
			console.log(info);
		},
	},
	getters: {
		//详细地址
		addrDetail(state){
			if(!state.address.userName){
				//所选地址为空对象
				return '';
			}
			
			return state.address.provinceName + state.address.cityName + state.address.countyName + state.address.detailInfo;
		},
	},
}